<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="../bootstrap-4.5.3/css/bootstrap.css" />
    <link rel="stylesheet" href="../css/common.css" />
</head>

<body>
    <!-- 面包屑 -->
    <div class="my-breadcrumb">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="#">首页</a></li>
                <li class="breadcrumb-item"><a href="#">用户管理</a></li>
                <li class="breadcrumb-item active">学生管理</li>
            </ol>
        </nav>
    </div>
    <div class="my-button">
        <!-- 按钮 -->
        <button class="btn btn-primary btn-lg" data-type="add" onclick="handlerOpen(this)">
            新增用户
        </button>
    </div>
    <div class="my-table">
        <!-- 表格 -->
        <table class="table table-bordered table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>学生id</th>
                    <th>姓名</th>
                    <th>头像</th>
                    <th>年龄</th>
                    <th>成绩</th>
                    <th>入学时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="tableTemplateNode">
                <script id="tableTemplate" type="text/html">
                    {{each stus as stu}}
                    <tr>
                        <td>{{stu.id}}</td>
                        <td>{{stu.name}}</td>
                        <td>
                            <img width="100" height="50" src="http://lpy.web44.com:8080{{stu.image}}" alt="暂无头像" />
                        </td>
                        <td>{{stu.age}}</td>
                        <td>{{stu.score}}</td>
                        <td>{{stu.comedate}}</td>
                        <td>
                            <button class="btn btn-success btn-sm " data-type="edit" data-id="{{stu.id}}"
                                onclick="handlerOpen(this)">
                                修改
                            </button>
                            <button class="btn btn-danger btn-sm ">删除</button>
                        </td>
                    </tr>
                    {{/each}}
                </script>
            </tbody>
        </table>
    </div>
    <div class="my-page">
        <!-- 分页 -->
        <nav class="float-right">
            <ul class="pagination">
                <li class="page-item"><a class="page-link" href="#">上一页</a></li>
                <li class="page-item"><a class="page-link" href="#">1</a></li>
                <li class="page-item"><a class="page-link" href="#">2</a></li>
                <li class="page-item"><a class="page-link" href="#">3</a></li>
                <li class="page-item"><a class="page-link" href="#">下一页</a></li>
            </ul>
        </nav>
    </div>

    <!-- 模态弹出框 -->
    <div id="addOrEdit" class="modal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content" id="addEditNode">
                <script type="text/html" id="addEditTemplate">
                    <div class="modal-header">
                        {{if type=='add'}}
                        <h5 class="modal-title">新增</h5>
                        {{else}}
                        <h5 class="modal-title">修改</h5>
                        {{/if}}
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form id="editForm">
                            <div class="form-group row align-items-center">
                                <label for="stuid" class="col-2">学生id:</label>
                                <input type="text" class="form-control col-9" 
                                id="stuid" name="id" value="{{id}}"
                                    placeholder="请输入学生id" />
                            </div>
                            <div class="form-group row align-items-center">
                                <label for="stuname" class="col-2">学生姓名:</label>
                                <input type="text" name="name" class="form-control col-9" id="stuname"
                                    value="{{name}}" />
                            </div>

                            <div class="form-group row align-items-center">
                                <label for="stuimage" class="col-2">头像:</label>
                                <!-- <input type="text" class="form-control col-9" id="stuimage" 
                            placeholder="请输入密码"> -->
                                <input type="file" id="fileNode" style="display: none;" name="image">
                                <img src="http://lpy.web44.com:8080{{image}}" 
                                id="imageNode" onclick="handlerOpenImage(this)"
                               
                                 alt="暂无图片" width="300"
                                    height="200" />

                                <input type="hidden" id="imagefileNode" 
                                name="image" value="{{image}}">
                            </div>
                            <div class="form-group row align-items-center">
                                <label for="stuage" class="col-2">年龄:</label>
                                <input type="text" name="age"
                                 class="form-control col-9" id="stuage" value="{{age}}" />
                            </div>
                            <div class="form-group row align-items-center">
                                <label for="stuscore" class="col-2">成绩:</label>
                                <input type="text" name="score" 
                                class="form-control col-9" id="stuscore"
                                    value="{{score}}" />
                            </div>
                            <div class="form-group row align-items-center">
                                <label for="stucomedate" class="col-2">入学时间:</label>
                                <input type="date" name="comedate"
                                 class="form-control col-9" id="stucomedate"
                                    value="{{comedate}}" />
                            </div>
                            <div class="form-group row align-items-center">
                                <label for="sturemark" class="col-2">备注:</label>
                                <textarea name="remark">
                                    {{remark}}
                                </textarea>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" onclick="handlerSave()">保存</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">
                            关闭
                        </button>
                    </div>
                </script>
            </div>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.2.4/jquery.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/art-template/4.13.2/lib/template-web.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="../bootstrap-4.5.3/js/bootstrap.js"></script>
    <script src="../utils/tools.js"></script>

    <script src="../utils/tools.js"></script>

    <script>
        loaddata();

        function loaddata() {
            $.ajax({
                url: obj.baseUrl + "/stu?method=queryStus",
                success: function (result) {
                    // console.log(result);
                    if (result.status == 200) {
                        console.log(result.data);
                        var stus = result.data;
                        console.log(stus);
                        var html = template("tableTemplate")({
                            stus: stus,
                        });

                        tableTemplateNode.innerHTML = html;
                    } else {
                        alert(result.msg);
                    }
                },
            });
        }

        window.stuObj = {
            id: "",
            name: "",
            image: "",
            age: "",
            score: "",
            comedate: "",
            remark: "",
            type: "", //add 新增 edit 修改
        };

        /**
         * 弹出模态窗口
         */
        function handlerOpen(obj) {
            window.stuObj.type = obj.dataset.type;
            window.stuObj.id = obj.dataset.id;
            console.log(window.stuObj.id);


            $("#addOrEdit").modal("show");
        }

        /**
         * 模态窗体打开之前调用此事件
         */
        $("#addOrEdit").on("shown.bs.modal", function (event) {
            // 定义回显信息
            console.log(window.stuObj.type);
            // 判断 新增 修改
            if (window.stuObj.type === "add") {
                window.stuObj = {
                    id: "",
                    name: "",
                    image: "",
                    age: "",
                    score: "",
                    comedate: "",
                    remark: "",
                    type: "add", //add 新增 edit 修改
                };
                // console.dir(obj.dataset.type);
                var html = template("addEditTemplate")(window.stuObj);
                addEditNode.innerHTML = html;
            } else {
                $.ajax({
                    url: obj.baseUrl + "/stu?method=queryStu&id=" + window.stuObj.id,
                    success: function (result) {
                        // console.log(result);
                        if (result.status == 200) {

                            window.stuObj = result.data
                            window.stuObj.type = "edit"

                            // console.dir(obj.dataset.type);
                            var html = template("addEditTemplate")(window.stuObj);
                            addEditNode.innerHTML = html;



                        } else {
                            alert(result.msg);
                        }
                    },
                });

            }
        });

        function handlerOpenImage(o) {
            fileNode.click()

            fileNode.onchange = function () {
                // 自定义的表单数据
                var formData = new FormData();
                formData.append("image", this.files[0])

                $.ajax({
                    url: obj.baseUrl + "/uploadfile?method=upload",
                    type: "post", //content-type:x-www-form-u
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function (result) {
                        // console.log(result);
                        if (result.status == 200) {
                            var path = decodeURIComponent(result.data)
                            console.log(path);
                            // console.log(result.data);
                            o.src = obj.baseUrl+path
                            o.setAttribute("src",obj.baseUrl+path)
                            // 提交的数据
                            imagefileNode.value = path
                            // 展示的图片
                            window.stuObj.image =path

                  

                      
                      

                        } else {
                            alert(result.msg);
                        }
                    },
                });

            }

        }
        
        /**
         * 保存
         */
        function handlerSave() {

           var dataStr =  decodeURI($("#editForm").serialize()) 
      
           var dataObj = transeformParams(dataStr)
           dataObj.isAdd = window.stuObj.type;
           console.log(dataObj);

           $.ajax({
                    url: obj.baseUrl + "/stu?method=save",
                    type: "post", //content-type:x-www-form-u
                    data: dataObj,
             
                    success: function (result) {
                        // console.log(result);
                        if (result.status == 200) {
                            loaddata();
                            alert("保存成功！")
                            $("#addOrEdit").modal("hide");

                        } else {
                            alert(result.msg);
                        }
                    },
                });

           

        }
    </script>
</body>

</html>